package com.learn.english.service.impl;

import com.learn.english.enums.TimeEnum;
import com.learn.english.mapper.CourseVideoMapper;
import com.learn.english.model.entity.CourseVideo;
import com.learn.english.service.IVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.learn.english.service.RedisService;

import java.util.List;

/**
 * 视频服务实现类
 */
@Slf4j
@Service
public class VideoServiceImpl implements IVideoService {

    @Autowired
    private CourseVideoMapper videoMapper;
    
    @Autowired
    private RedisService redisService;
    
    private static final String VIDEO_COUNT_CACHE_KEY = "video:count";

    @Override
    public void playVideo(String filename) {
        // 记录视频播放并更新播放次数
        log.info("Video played: {}", filename);
        int result = videoMapper.playVideo(filename);
        if (result <= 0) {
            log.warn("Failed to update play count for video: {}", filename);
        }
    }
    
    @Override
    public Long getVideoCount() {
        // 先从缓存获取
        Long count = redisService.getCacheObject(VIDEO_COUNT_CACHE_KEY, Long.class);
        if (count == null || count == 0) {
            // 缓存中没有，则从数据库获取
            count = videoMapper.selectCount(null);
            if (count > 0) {
                // 写入缓存，设置过期时间
                redisService.setCacheObject(VIDEO_COUNT_CACHE_KEY, count, TimeEnum.FOUR_HOUR);
            }
        }
        return count;
    }
    
    @Override
    public CourseVideo upload(CourseVideo courseVideo) {
        // 记录视频上传信息
        log.info("Video uploaded: courseId={}, title={}", courseVideo.getCourseId(), courseVideo.getTitle());
        
        // 设置默认值
        if (courseVideo.getStatus() == null) {
            courseVideo.setStatus(1); // 默认启用
        }
        if (courseVideo.getIsFree() == null) {
            courseVideo.setIsFree((byte) 0); // 默认付费
        }
        
        // 保存到数据库
        int result = videoMapper.insertVideo(courseVideo);
        if (result <= 0) {
            log.error("Failed to save video: courseId={}, title={}", courseVideo.getCourseId(), courseVideo.getTitle());
            throw new RuntimeException("Failed to save video");
        }
        
        return courseVideo;
    }
    
    @Override
    public List<CourseVideo> getVideoListByChapterId(Long chapterId) {
        log.info("Get video list by chapterId: {}", chapterId);
        return videoMapper.findByChapterId(chapterId);
    }
    
    @Override
    public CourseVideo getVideoById(Long id) {
        log.info("Get video by id: {}", id);
        return videoMapper.findById(id);
    }
    
    @Override
    
    public boolean updateVideo(CourseVideo courseVideo) {
        log.info("Update video: id={}, title={}", courseVideo.getId(), courseVideo.getTitle());
        int result = videoMapper.updateVideo(courseVideo);
        return result > 0;
    }
    
    @Override
    
    public boolean deleteVideo(Long id) {
        log.info("Delete video: id={}", id);
        int result = videoMapper.deleteVideo(id);
        return result > 0;
    }
    
    @Override
    
    public boolean deleteByChapterId(Long chapterId) {
        log.info("Delete videos by chapterId: {}", chapterId);
        int result = videoMapper.deleteByChapterId(chapterId);
        return result > 0;
    }
    
    @Override
    
    public boolean updateSort(Long id, Integer sort) {
        log.info("Update video sort: id={}, sort={}", id, sort);
        int result = videoMapper.updateSort(id, sort);
        return result > 0;
    }
}